package org.ling.xuan.util;

import java.util.function.Function;

public class LxOptional<T> {
    T t;

    public LxOptional(T t) {
        this.t = t;
    }

    public static <T> LxOptional<T> of(T t) {
        return new LxOptional<>(t);
    }

    public static <R, T> LxOptional<T> of(R r, Function<R, T> function) {
        try {
            return new LxOptional<>(function.apply(r));
        } catch (NullPointerException e) {
            return new LxOptional<>(null);
        }
    }
    public boolean isPresent() {
        return t != null;
    }
    public T orElse(T t1) {
        if (isPresent()) {
            return t;
        } else {
            return t1;
        }
    }
    public T get() {
        return t;
    }

}

